import 'package:flutter/material.dart';
import 'package:haitao_app/home.dart';
import 'package:haitao_app/index.dart';
import 'dart:async';
import 'dart:ui';

import 'package:haitao_app/mainIndex.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Welcome(),
    );
  }

}

class Welcome extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return WelcomeWidget();
  }
  
}

class WelcomeWidget extends State<Welcome>{

  Timer timer;
  double maxTime=0;
  double changeTimer=0;
  
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    timer=Timer.periodic(Duration(milliseconds: 50), (t){
          changeTimer++;
            setState(() {
              changeTimer=changeTimer;
              if(changeTimer%20==0){
                maxTime=changeTimer/20;
              }
            });
          if(maxTime==5){
            //跳转
            timer?.cancel();
            timer=null;
            Navigator.pushAndRemoveUntil(context,
            MaterialPageRoute(
                builder: (context)=>MainIndex()
            ),
            (Route route)=> route==null );
          }

    });
  }
  
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: Stack(
        children: <Widget>[
          Container(
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height,
            child: Image.asset("images/text.jpg",fit: BoxFit.fill,),
          ),
          Container(
            alignment: Alignment.bottomRight,
            margin: EdgeInsets.only(bottom: 20, right: 20),
            child: Container(
              height: 50,
              width: 50,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                  border: Border.all(color: Colors.black54),
                  borderRadius: BorderRadius.all(Radius.circular(25)),
                  color: Colors.white
              ),
              child: Stack(
                children: <Widget>[
                  Container(
                    width: 50,
                    height: 50,
                    alignment: Alignment.center,
                    child: CircularProgressIndicator(
                      value: 1-changeTimer/100,
                      strokeWidth: 3,
                    ),
                  ),
                  Container(
                    alignment: Alignment.center,
                    child: Text("${5-maxTime}"),
                  )
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }
  
}

